--
-- Author: yawei.zhu
-- Date: 2018-12-13 16:00:00
--
-- TaskTrackGrayRender
--

local ClassRef = app.mvc.createListViewCell(...)

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.TrackTaskItemNodeGrayBattle))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)
    self:setContentSize(self._ui.Panel:getContentSize())
    self._ui["Panel/button"]:addClickEventListener(function()
        self:dispatchCellEvent("openGrayBattle")
    end)

    for i = 1, 4 do
        self._ui["Panel/Panel_icon/icon"..i]:loadTexture(Res.npcIconPath..GrayBattleSoulIcons[i])
    end
    self:refresh()
end

function ClassRef:refresh()
    if not self._data then return end

    local grayBattlePanel = self._ui.Panel

    local panelIcon = self._ui["Panel/Panel_icon"]
    local hintText = self._ui["Panel/Text"]

    self._ui["Panel/bg_finish"]:setVisible(false)
    self._ui["Panel/bg_fail"]:setVisible(false)

    if UD:isGrayBattleFinished() then
        panelIcon:setVisible(false)
        hintText:setVisible(true)
        local rewardGrade = UD:getGrayBattleRewardGrade()
        if rewardGrade == Enums.activityRewardGrade.none then
            self._ui["Panel/bg_finish"]:setVisible(false)
            self._ui["Panel/bg_fail"]:setVisible(true)
            hintText:setString(L("activity_ashes_fail"))
        else
            self._ui["Panel/bg_finish"]:setVisible(true)
            self._ui["Panel/bg_fail"]:setVisible(false)
            if rewardGrade == Enums.activityRewardGrade.bronze then
                hintText:setString(L("activity_ashes_success_bronze"))
            elseif rewardGrade == Enums.activityRewardGrade.silver then
                hintText:setString(L("activity_ashes_success_silver"))
            elseif rewardGrade == Enums.activityRewardGrade.gold then
                hintText:setString(L("activity_ashes_success_gold"))
            end
        end
    else
        panelIcon:setVisible(true)
        hintText:setVisible(false)
        for i = 1, 4 do
            self._ui["Panel/Panel_icon/icon"..i]:setVisible(false)
        end
        local soulList = UD:getGrayBattleSoulList() or {}
        for _, soulType in ipairs(soulList) do
            if self._ui["Panel/Panel_icon/icon"..soulType] ~= nil then
                self._ui["Panel/Panel_icon/icon"..soulType]:setVisible(true)
            end
        end
    end
end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end



return ClassRef
